//
//  MyTabBar.m
//  WYCweibo
//
//  Created by 魏云超 on 15/11/12.
//  Copyright © 2015年 魏云超. All rights reserved.
//

#import "MyTabBar.h"
#import "MyTabBarButton.h"
#import <CoreImage/CoreImage.h>
@interface MyTabBar()
//当前选中按钮
@property(nonatomic,weak)UIButton *currentSelectedBtn;
@end

@implementation MyTabBar

-(void)addTabBarButtonWith:(UITabBarItem *)item
{
    MyTabBarButton  *btn = [[MyTabBarButton alloc]init];
    [self addSubview:btn];
    //设置图片
    [btn setBackgroundImage:item.image forState:UIControlStateNormal];
    [btn setBackgroundImage:item.selectedImage forState:UIControlStateSelected];
    //监听按钮的点击事件
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    //设置默认选中
    NSInteger count = self.subviews.count;
    
    if(count == 1){
        [self buttonClick:btn];
    }
}
-(void)layoutSubviews{
    NSInteger count = self.subviews.count;
    CGFloat btnW = self.frame.size.width / 2;
    CGFloat btnH = self.frame.size.height;
    
    for (int i = 0; i<count; i++) {
        //取出对应的按钮
        UIButton *btn  = self.subviews[i];
        //设置五个按钮的frame
        CGFloat btnY = 0;
        CGFloat btnX = i * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        //给按钮设置tag
        btn.tag = i;
    }
}
-(void)buttonClick:(UIButton *) btn{
    DDLogDebug(@"点击了");
}
//通过代码创建
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        //初始化按钮
        //[self setupBtn];
    }
    return self;
}
//通过xib storyboard创建
-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        //初始化按钮
        //[self setupBtn];
    }
    return self;
}
@end
